<?php
/**
 +------------------------------------------------------------------------------
 * PrintAction控制器类
 +------------------------------------------------------------------------------
 * @category   打印操作
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <admin@iic.cn>
 * @date 2013-9-13
 * @time  下午10:15:25
 +------------------------------------------------------------------------------
 */
class PrintAction extends BaseAction {
	protected $line, $ods,$areaall;
	function _initialize() {
		parent::_initialize ();
		import ( 'Com.OrderSelect' );
		$this->ods = new OrderSelect ();
		$this->orderdao = M ( 'Order' );
		$status = array ();
		$status ['0'] = '待审核';
		$status ['1'] = '已审核';
		$status ['2'] = '已完成';
		$status ['-2'] = '已退款';
		$status ['3'] = '备货中';
		$status ['4'] = '已退货';
		$status ['5'] = '已发货';
		$status ['-5'] = '已退货';
		$status ['-1'] = '审核不通过';
		$status ['8'] = '已签收';
		$status ['10'] = '已完成';
		$status ['-10'] = '已取消';
		$this->assign ( 'status', $status );
		$pay_status = array ();
		$pay_status ['0'] = '未支付';
		$pay_status ['1'] = '付款中';
		$pay_status ['2'] = '已付款';
		$pay_status ['3'] = '申请退款';
		$pay_status ['4'] = '己退款';
		$this->assign ( 'pay_status', $pay_status );
		$shipping_status = array ();
		$shipping_status ['0'] = '未发货';
		$shipping_status ['1'] = '已发货';
		$shipping_status ['2'] = '已收货';
		$shipping_status ['3'] = '申请换货';
		$shipping_status ['4'] = '己换货';
		$this->assign ( 'shipping_status', $shipping_status );
		$this->areaall = $this->area();
		$this->assign('areaall',$this->areaall);
	} // end _initialize
	
	/**
	 * 列出有效订单及打印操作选项
	 * @date 2013-9-14
	 * @time 下午3:56:02
	 */
	function o2o() {
		// 列出有效订单及打印操作选项
		$sel_status = $_REQUEST ['sel_status'] ? $_REQUEST ['sel_status'] : 'ALL';
		$this->assign ( 'sel_status', $sel_status );
		$where = array ();
		$where ['platform'] = '91YUNGOU';
		if ($sel_status != 'ALL') {
			$where ['status']=$sel_status;
		}else{
			//默认只显示审核通过的订单
			$where ['status']=array('gt',0);
		}
		$count = $this->ods->order_list ( $where, 'all' );
		$this->assign ("totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$pages = $page->show ();
			$list = $this->ods->order_list ( $where, $page->firstRow . ',' . $page->listRows, $this->order );
			$list = field2key ( $list, 'id' );
			$list = $this->ods->order_ext ( $list );
			$this->assign ( 'list', $list );
		}
		$this->display ();
	} // end index
	
	/**
	 * 云采购的订单
	 * @date 2013-6-21
	 * @time 下午2:46:37
	 */
	function smw() {
		// 云采购的订单
		$sel_status = $_REQUEST ['sel_status'] ? $_REQUEST ['sel_status'] : 'ALL';
		$this->assign ( 'sel_status', $sel_status );
		$where = array ();
		$where ['platform'] = 'SMW01';
		if ($sel_status != 'ALL') {
			$where ['status'] = $sel_status;
		}
		$count = $this->ods->order_list ( $where, 'all' );
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$pages = $page->show ();
			$list = $this->ods->order_list ( $where, $page->firstRow . ',' . $page->listRows );
			$this->assign ( 'list', $list );
		}
		$oids = arr_to_explode ( $list, 'id' );
		$depot = self::getdepot ( $oids );
		$this->assign ( 'depot', $depot );
		$this->display ();
	} // end smw

	
	/**
	 * 雲採購出庫地址單打印
	 * @date 2013-9-13
	 * @time 下午10:18:27
	 */
	function smw_psdprint() {
		// 雲採購出庫地址單打印
		$ases = array ();
		$coupon = array();
		if(!empty ($_REQUEST ['ids'])){
			$ids =  explode(',',$_REQUEST['ids']);
			foreach ( $ids as $id ) {
				$ases [] = $this->ods->order_detail ( $id );
				
				/**********begin 免邮 **********/
				$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
				if($coupondata[0]['value']==1){
					$coupon[$id] = 1;
				}
				
			}
		}else{
			$id = $_REQUEST ['id'];
			$ases[] = $this->ods->order_detail ( $id );

			/**********begin 免邮 **********/
			$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
			if($coupondata[0]['value']==1){
				$coupon[$id] = 1;
			}
		
		}
		
		
		//*** begin  对缺货商品，重新计算数量和价格。********/
		
		foreach ($ases as $key => $o){
			foreach ($o['goods'] as $gkey => $vog){
				$num = 0;
				foreach ($o['finance'] as $vof){
					if(($vof['action']=='OOS' || $vof['REFUND']=='REFUND') && $vof['goods_id'] == $vog['product_id']){
						//减掉缺货后的数量
						$num +=  $vof['number'];
						if($vog['number'] - $num <= 0){
							//没有货，则不显示商品
							unset($ases[$key]['goods'][$gkey]);
							break;
						}else{
							$ases[$key]['goods'][$gkey]['number'] = $vog['number'] - $num;
							$ases[$key]['goods'][$gkey]['priceall'] = ($vog['number'] - $num)*$vog['price'];
						}
					}
				}
			}
				
		}
		
		//*** end  对缺货商品，重新计算数量和价格。*********/

		
		//快递单合单
		if($_REQUEST['merge'] == 1){
			for($i=0;$i<count($ases);$i++){
				$arr1 = $ases[$i];
				$address1 = $arr1['address'];
				if($arr1['merge'] == 1){
					continue;
				}
				for ($j=$i+1;$j<count($ases);$j++){
					$arr2 = $ases[$j];
					
					
					if($arr1['userid'] == $arr1['userid'] && $address1['province'] == $arr2['address']['province'] && $address1['city'] == $arr2['address']['city'] 
					&& $address1['district'] == $arr2['address']['district'] && $address1['address'] == $arr2['address']['address']){	
						if(!$ases[$i]['ids']){
							$ases[$i]['ids'] = $ases[$i]['id'];
						}					
						$ases[$i]['ids'] = $ases[$i]['ids'].','.$arr2['id'];
						$ases[$i]['amount'] = $ases[$i]['amount']+$arr2['amount'];
						$ases[$i]['count_num'] = $ases[$i]['count_num']+$arr2['count_num'];
						$ases[$i]['goods'] =array_merge($ases[$i]['goods'] , $ases[$j]['goods']) ;
						$ases[$j]['merge'] = 1;
					}
					
				}
			}
			foreach ($ases as $key=>$vo){
				if($vo['merge'] == 1){
					unset($ases[$key]);
				}
			}
		}
		//print_r($ases);
		$this->assign('coupon',$coupon);
		$this->assign ( "ases", array_reverse($ases) );
		$this->display ();
	} // end smw_psdprint
	
	
	
	
	/**
	 * 雲採購出庫地址單打印
	 * @date 2013-9-13
	 * @time 下午10:18:27
	 */
	function smw_psdprint_test() {
		// 雲採購出庫地址單打印
		$ases = array ();
		$coupon = array();
		if(!empty ($_REQUEST ['ids'])){
			$ids =  explode(',',$_REQUEST['ids']);
			foreach ( $ids as $id ) {
				$ases [] = $this->ods->order_detail ( $id );
	
				/**********begin 免邮 **********/
				$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
				if($coupondata[0]['value']==1){
					$coupon[$id] = 1;
				}
	
			}
		}else{
			$id = $_REQUEST ['id'];
			$ases[] = $this->ods->order_detail ( $id );
	
			/**********begin 免邮 **********/
			$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
			if($coupondata[0]['value']==1){
				$coupon[$id] = 1;
			}
	
		}
	
		//	print_r($ases);
		//快递单合单
		if($_REQUEST['merge'] == 1){
			for($i=0;$i<count($ases);$i++){
				$arr1 = $ases[$i];
				$address1 = $arr1['address'];
				if($arr1['merge'] == 1){
					continue;
				}
				for ($j=$i+1;$j<count($ases);$j++){
					$arr2 = $ases[$j];
						
						
					if($arr1['userid'] == $arr1['userid'] && $address1['province'] == $arr2['address']['province'] && $address1['city'] == $arr2['address']['city']
					&& $address1['district'] == $arr2['address']['district'] && $address1['address'] == $arr2['address']['address']){
						if(!$ases[$i]['ids']){
							$ases[$i]['ids'] = $ases[$i]['id'];
						}
						$ases[$i]['ids'] = $ases[$i]['ids'].','.$arr2['id'];
						$ases[$i]['amount'] = $ases[$i]['amount']+$arr2['amount'];
						$ases[$i]['count_num'] = $ases[$i]['count_num']+$arr2['count_num'];
						$ases[$i]['goods'] =array_merge($ases[$i]['goods'] , $ases[$j]['goods']) ;
						$ases[$j]['merge'] = 1;
					}
						
				}
			}
			foreach ($ases as $key=>$vo){
				if($vo['merge'] == 1){
					unset($ases[$key]);
				}
			}
		}
	
		$this->assign('coupon',$coupon);
		$this->assign ( "ases", array_reverse($ases) );
		$this->display ();
	} // end smw_psdprint
	
	
	
	/**
	 * 云采购订单出库单打印
	 *@date 2013-10-16
	 *@time 下午3:41:00
	 */
	function smw_print() {
		//云采购订单出库单打印
		$ases = array ();
		if(!empty ($_REQUEST ['ids'])){
			$ids =  explode(',', $_REQUEST['ids']);
			$coupon = array();
			foreach ( $ids as $id ) {
				$ases [] = $this->ods->order_detail ( $id );
				$coupondata[$id] = M ()->query ( "select *	from order_coupon where order_id = {$id}" );
				/**********begin 老郭专场 标识  add by tb **********/		
				foreach ($coupondata[$id] as $c){
					if($c['subject'] == 'CV' && $c['type'] == 'USE'){
						$coupon[$id][$c['goods_id']]['USE'] = 1;
					}
				}
				/**********end 老郭专场 标识  add by tb **********/
				

				/**********begin 免邮 **********/
				$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
				if($coupondata[0]['value']==1){
					$coupon[$id]['free'] = 1;
				}
			}
		}else{
			$id = $_REQUEST ['id'];
			$ases[] = $this->ods->order_detail ( $id );
			$coupondata[$id] = M ()->query ( "select *	from order_coupon where order_id = {$id}" );
			/**********begin 老郭专场 标识  add by tb **********/
			$coupon = array();
			foreach ($coupondata[$id] as $c){
				if($c['subject'] == 'CV' && $c['type'] == 'USE'){
					$coupon[$id][$c['goods_id']]['USE'] = 1;
				}
			}
			/**********end 老郭专场 标识  add by tb **********/
			
			/**********begin 免邮 **********/
			$coupondata = M ()->query("select * from order_ext where order_id = {$id} and element = 'free'");
			if($coupondata[0]['value']==1){
				$coupon[$id]['free'] = 1;
			}
		}
		
		
		//*** begin  对缺货商品，重新计算数量和价格。********/
	
		foreach ($ases as $key => $o){
			foreach ($o['goods'] as $gkey => $vog){
				$num = 0;
				foreach ($o['finance'] as $vof){
					if(($vof['action']=='OOS' || $vof['REFUND']=='REFUND') && $vof['goods_id'] == $vog['product_id']){
						//减掉缺货后的数量						
						$num +=  $vof['number'];
						if($vog['number'] - $num <= 0){
							//没有货，则不显示商品
							unset($ases[$key]['goods'][$gkey]);
							break;
						}else{						
							$ases[$key]['goods'][$gkey]['number'] = $vog['number'] - $num;
							$ases[$key]['goods'][$gkey]['priceall'] = ($vog['number'] - $num)*$vog['price'];					
						}
					}
				}
			}
			
		}
		
		//*** end  对缺货商品，重新计算数量和价格。*********/
				
		/**************** begin 发货单合单 $_REQUEST['merge'] =1 **********************/
		if($_REQUEST['merge'] == 1){
			/**** begin 合并订单  ****/
			for($i=0;$i<count($ases);$i++){
				$arr1 = $ases[$i];
				$address1 = $arr1['address'];
				//被合并的集合将不再循环
				if($arr1['merge'] == 1){
					continue;
				}
				for ($j=$i+1;$j<count($ases);$j++){
					$arr2 = $ases[$j];
		
					if($arr1['userid'] == $arr1['userid'] && $address1['province'] == $arr2['address']['province'] && $address1['city'] == $arr2['address']['city']
					&& $address1['district'] == $arr2['address']['district'] && $address1['address'] == $arr2['address']['address']){
						if(!$ases[$i]['ids']){
							$ases[$i]['ids'] = $ases[$i]['id'];
						}
						$ases[$i]['amount'] += $ases[$j]['amount']; //计算合并订单后商品总额
						$ases[$i]['count_num'] += $ases[$j]['count_num']; //计算合并订单后商品总数
						$ases[$i]['ids'] = $ases[$i]['ids'].','.$arr2['id'];
						$ases[$i]['goods'] =array_merge($ases[$i]['goods'] , $ases[$j]['goods']) ;
						$ases[$j]['merge'] = 1; //标记为已经合并的数组。
					}
						
				}
			}
			/**** end 合并订单  ****/
		
			/**** begin 合并商品 ****/
			$addamount = 0 ;
			foreach ($ases as $key=>$vo){
				if($vo['merge'] == 1){
					unset($ases[$key]);					
				}				
//				if($vo['ids'] && !$vo['merge']){
// 					$goods = $vo['goods'];
// 					for($i=0 ; $i<count($goods); $i++){
						
// 						//被合并的集合将不再循环
// 						if($goods['merge'] == 1){
// 							continue;
// 						}						
// 						for($j=$i+1; $j<count($goods);$j++){
// 							if($goods[$i]['product_id'] == $goods[$j]['product_id']){
// 								$goods[$i]['priceall'] += $goods[$j]['priceall']; //总金额								
// 								$goods[$i]['number'] += $goods[$j]['number']; // 总数量
// 								$goods[$j]['merge'] = 1; //标记被上条商品合并															
// 							}
// 						}						
// 					}
// 					foreach ($goods as $gkey => $gvo){
// 						if($gvo['merge'] == 1){
// 							unset($goods[$gkey]);
// 						}
// 					}
// 					$ases[$key]['goods'] = $goods;
//					
//				}
				
			}
			/**** end 合并商品 ****/
		}
		/**************** end 发货单合单 $_REQUEST['merge'] =1 *********************/
		
		
		/**父订单ID**/

		$this->assign ( 'coupondata',$coupondata);
		$this->assign ( "ases", array_reverse ( $ases ) );
		$this->assign('coupon',$coupon);
		$this->display ();
	}//end smw_print
	/**
	 * 云采购订单出库单打印
	 *@date 2013-10-16
	 *@time 下午3:41:00
	 */
	function smw_print_new() {
		//云采购订单出库单打印
		$ases = array ();
		if(!empty ($_REQUEST ['ids'])){
			$ids =  explode(',', $_REQUEST['ids']);
			$coupon = array();
			foreach ( $ids as $id ) {
				$ases [] = $this->ods->order_detail ( $id );
				$coupondata[$id] = M ()->query ( "select *	from order_coupon where order_id = {$id}" );
				/**********begin 老郭专场 标识  add by tb **********/
				foreach ($coupondata[$id] as $c){
					if($c['subject'] == 'CV' && $c['type'] == 'USE'){
						$coupon[$id][$c['goods_id']]['USE'] = 1;
					}
				}
				/**********end 老郭专场 标识  add by tb **********/
			}
		}else{
			$id = $_REQUEST ['id'];
			$ases[] = $this->ods->order_detail ( $id );
			$coupondata[$id] = M ()->query ( "select *	from order_coupon where order_id = {$id}" );
			/**********begin 老郭专场 标识  add by tb **********/
			$coupon = array();
			foreach ($coupondata[$id] as $c){
				if($c['subject'] == 'CV' && $c['type'] == 'USE'){
					$coupon[$id][$c['goods_id']]['USE'] = 1;
				}
			}
			/**********end 老郭专场 标识  add by tb **********/
		}
	
	
		//*** begin  对缺货商品，重新计算数量和价格。********/
		foreach ($ases as $key => $o){
			foreach ($o['goods'] as $gkey => $vog){
				foreach ($o['finance'] as $vof){
					if(($vof['action']=='OOS' || $vof['REFUND']=='REFUND') && $vof['goods_id'] == $vog['product_id']){
						//减掉缺货后的数量
						$num = $vog['number'] - $vof['number'];
						if($num <= 0){
							//没有货，则不显示商品
							unset($ases[$key]['goods'][$gkey]);
							break;
						}else{
							$ases[$key]['goods'][$gkey]['number'] = $num;
							$ases[$key]['goods'][$gkey]['priceall'] = $num*$vog['price'];
							break;
						}
					}
				}
			}
				
		}
	
		//*** end  对缺货商品，重新计算数量和价格。*********/
	
	
		$this->assign ( 'coupondata',$coupondata);
		$this->assign ( "ases", array_reverse ( $ases ) );
		$this->assign('coupon',$coupon);
		$this->display ();
	}//end smw_print
	/**
	 * 打印O2O出库单
	 * @date 2013-10-12
	 * @time 下午3:53:07
	 */
	function o2o_print() {
		// O2O订单打印
		$ids = !empty ($_REQUEST ['ids']) ? explode(',', $_REQUEST['ids']) : $_REQUEST ['id'];
		$ases = array ();
		foreach ( $ids as $id ) {
			$ases [] = $this->ods->o2o_order_detail ( $id );
		}
		$this->assign ( "ases", array_reverse ( $ases ) );
		$this->display ();
	} // end o2o_print
	
	/**
	 * 打印O2O配送单
	 * @date 2013-10-12
	 * @time 下午3:53:07
	 */
	function o2o_psdprint() {
		$ods = !empty ($_REQUEST ['ids']) ? explode(',', $_REQUEST['ids']) : $_REQUEST ['id'];
		$ases = array ();
		foreach ( $ods as $od ) {
			$ases [] = $this->ods->o2o_order_detail ( $od, 'address,ext' );
		}
		$this->assign ( "ases",array_reverse ( $ases ));
		$this->display ();
	}
	
	/**
	 * (non-PHPdoc)
	 * 返回所有地址列表
	 * @see BaseAction::area()
	 */
	function area(){
		$model = M('Area');
		$addr = $model->select();
		return field2key($addr,'id');
	}

}//end PrintAction